from PyQt4.QtGui import QWidget,QTabWidget,QMainWindow,QAction,QKeySequence
from PyQt4 import QtCore
from KUI.ui_KCentralWidget import Ui_KCentralWidget 
from KCharts.KChartsWidget import KChartsWidget
from KUI.KDatabaseUI.KDatabaseWidget import KDatabaseWidget
from KUI.KSmartSearch.KChartCompleteDlg import KChartCompleteDlg
from KReport.KStockListBrowseWidget import KStockListBrowseWidget
from KUI.KSetting.KSettingsWidget import KSettingsWidget
from KUI.KStrategyUI.KBatchTestWidget import KBatchTestWidget
import pyqtgraph as pg
from pyqtgraph.dockarea import *
from KReport import KStocksItemModel, KStocksTableView

from KNonTrade import KNonTradeMainWidget

class KCentralWidget(QTabWidget):
	
	SymbolChanged=QtCore.pyqtSignal(str) # triggerd by smart look up
	def __init__(self, parent=None):
		super(KCentralWidget, self).__init__(parent)
		self.ui =  Ui_KCentralWidget()
		self.ui.setupUi(self) 
		self.clear()
		
		# add setting widget
		m_setwgt=KSettingsWidget(self)
		self.addTab(m_setwgt,'Settings')
		
		# add Datbase Manage Window
		m_dbwgt=KDatabaseWidget(self)
		self.addTab(m_dbwgt,"Datbase")
		
		# add Stock Chart Window
		chartab=QMainWindow()
		self.addTab(chartab,"Charting")
		area1=DockArea()
		
		
		# add backtesting widget
		mbatchwgt=KBatchTestWidget(self)
		self.addTab(mbatchwgt,'BackTest')
		
		flag=1
		if flag==1:
			self.m_charts=KChartsWidget()
			self.SymbolChanged.connect(self.m_charts.OnSymbolChanged) # pass along the symbol change information to chart window
			chartab.setCentralWidget(self.m_charts)   


		
		# add Report Window
		m_rpwgt=KStockListBrowseWidget(self,KStocksItemModel.KStocksItemModel())
		self.SymbolChanged.connect(m_rpwgt.SymbolChanged) # pass along the symbol change information to report window
		m_rpwgt.ReportSelctionChanged.connect(self.OnReportSelectionChanged)
		self.addTab(m_rpwgt,"Report")
		
				
	
		
		# add non trade related tab
		#mbatchwgt=KNonTradeMainWidget.KNonTradeMainWidget(self)
		#self.addTab(mbatchwgt,'Non Trade')		
		
		
		# setup the smart stock lookup window
		self.completewgt=KChartCompleteDlg() 
		self.completewgt.SybmolObtained.connect(self.SymbolChanged) # get informed when symbol changed from smart look up

	
		self.setCurrentIndex(2)	
		
	def OnReportSelectionChanged(self,symbol):
		self.m_charts.OnSymbolChanged(str(symbol))
		self.setCurrentIndex(2)
		
	def keyPressEvent(self,evt):
		if evt==QKeySequence.Find: # Ctrl+F		
			if self.completewgt.isHidden()==True:
				subwidth=200
				subheight=400
				self.completewgt.resize(subwidth,subheight)
				self.completewgt.move(self.x()+self.width()-subwidth-20,self.y()+self.height()-subheight-10)
				self.completewgt.exec_()
